package com.seocoo.worker.base.multiImage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seocoo.worker.R;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.sfh.lib.ui.MVVMActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by: ssp
 * Date: 2020/4/16 19:00
 * Description:  查看大图
 */
public class LookImageActivity extends MVVMActivity {

    private DoorPreviewViewPager pager;
    private ArrayList<RspImage> data = new ArrayList<>();
    private int position;

    public static void startUi(Context context, List<RspImage> imageList, int position) {
        Intent intent = new Intent(context, LookImageActivity.class);
        intent.putExtra("imageList", (Serializable) imageList);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = (ArrayList<RspImage>) getIntent().getSerializableExtra("imageList");
        position = getIntent().getIntExtra("position", 0);
        setContentView(R.layout.preview_image_activity);
        pager = findViewById(R.id.pager);
        initAdapter();
    }

    private void initAdapter() {
        AdapterViewpager viewpager = new AdapterViewpager(data);
        pager.setAdapter(viewpager);
        pager.setCurrentItem(position);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    class AdapterViewpager extends PagerAdapter {
        List<RspImage> imageList;

        public AdapterViewpager(List<RspImage> list) {
            imageList = list;
        }

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(LookImageActivity.this).inflate(R.layout.image_preview_pager_item, container, false);
            PhotoView photoView = view.findViewById(R.id.photo_view);
            //  data
            RspImage rspImage = data.get(position);
            new ImageLoaderEngine(rspImage.fullPath).into(photoView);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }
}


